{
 "metadata": {
  "name": "",
  "signature": "sha256:278e046efbf447c0419efcb74e9ce660ca855e4a9aeef3da10a80cb3ca4e11e6"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Installation"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "```\n",
      "pip install reverse_geocoder\n",
      "```\n",
      "OR\n",
      "```\n",
      "pip install --upgrade reverse_geocoder\n",
      "```"
     ]
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Import Library"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import reverse_geocoder as rg"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 32
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Geocoding a Single Coordinate"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import pprint\n",
      "pp = pprint.PrettyPrinter(indent=4)\n",
      "coordinate = (51.520328, -0.097712)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 33
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "result = rg.get(coordinate) # default mode = 2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 34
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "pp.pprint(result)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "{   'admin1': 'England',\n",
        "    'admin2': 'Greater London',\n",
        "    'cc': 'GB',\n",
        "    'lat': '51.51988',\n",
        "    'lon': '-0.09446',\n",
        "    'name': 'Barbican'}\n"
       ]
      }
     ],
     "prompt_number": 35
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Geocoding Multiple Coordinates"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "coordinates = (51.5214588,-0.1729636),(9.936033, 76.259952),(37.38605,-122.08385)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 36
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "results = rg.search(coordinates) # default mode = 2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 37
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "pp.pprint(results)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "[   {   'admin1': 'England',\n",
        "        'admin2': 'Greater London',\n",
        "        'cc': 'GB',\n",
        "        'lat': '51.51116',\n",
        "        'lon': '-0.18426',\n",
        "        'name': 'Bayswater'},\n",
        "    {   'admin1': 'Kerala',\n",
        "        'admin2': 'Ernakulam',\n",
        "        'cc': 'IN',\n",
        "        'lat': '9.93988',\n",
        "        'lon': '76.26022',\n",
        "        'name': 'Cochin'},\n",
        "    {   'admin1': 'California',\n",
        "        'admin2': 'Santa Clara County',\n",
        "        'cc': 'US',\n",
        "        'lat': '37.38605',\n",
        "        'lon': '-122.08385',\n",
        "        'name': 'Mountain View'}]\n"
       ]
      }
     ],
     "prompt_number": 38
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Single Process Geocoding"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "coordinates = (51.459185, -2.585109),(1.292697, 103.852033),(-27.450283, 153.016957)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 39
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "results_slow = rg.search(coordinates,mode=1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 40
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "pp.pprint(results_slow)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "[   {   'admin1': 'England',\n",
        "        'admin2': 'Bristol',\n",
        "        'cc': 'GB',\n",
        "        'lat': '51.45523',\n",
        "        'lon': '-2.59665',\n",
        "        'name': 'Bristol'},\n",
        "    {   'admin1': '',\n",
        "        'admin2': '',\n",
        "        'cc': 'SG',\n",
        "        'lat': '1.28967',\n",
        "        'lon': '103.85007',\n",
        "        'name': 'Singapore'},\n",
        "    {   'admin1': 'Queensland',\n",
        "        'admin2': 'Brisbane',\n",
        "        'cc': 'AU',\n",
        "        'lat': '-27.46141',\n",
        "        'lon': '153.02311',\n",
        "        'name': 'Spring Hill'}]\n"
       ]
      }
     ],
     "prompt_number": 41
    },
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "Geocoding from an Input File"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import csv\n",
      "input_filename = '/Users/ajay/Workspace/pydata_demo/coordinates_10000.csv'\n",
      "output_filename = '/Users/ajay/Workspace/pydata_demo/coordinates_10000_geocoded.csv'\n",
      "cities = [(row[0],row[1]) for row in csv.reader(open(input_filename,'rt'),delimiter='\\t')]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 42
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "pp.pprint(cities[0:5])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "[   ('13.9280531', '100.3735803'),\n",
        "    ('13.7233157', '-89.1415332'),\n",
        "    ('28.6440457', '-81.122403'),\n",
        "    ('35.9105783', '128.817739'),\n",
        "    ('43.2615353', '-2.9267777')]\n"
       ]
      }
     ],
     "prompt_number": 43
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "results = rg.search(cities)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 44
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "rows = []\n",
      "for idx, city in enumerate(cities):\n",
      "    write_row = []\n",
      "    lat = city[0]\n",
      "    lon = city[1]\n",
      "    gdata = results[idx]\n",
      "    rows.append([lat,lon,gdata['name'],gdata['admin1'],gdata['admin2'],gdata['cc']])\n",
      "csvwriter = csv.writer(open(output_filename,'wt'),delimiter='\\t')\n",
      "csvwriter.writerows(rows)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 45
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 45
    }
   ],
   "metadata": {}
  }
 ]
}